/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elsimulatorjava;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 *
 * @author corey
 */
public class ElevatorDialog extends JDialog {
    private JTable tableWidget;
    private JScrollPane tableView;
    private JButton clearB, submitB, fillB;
    private String[] tableColumns = {"Min Floor", "Max Floor", "Start Floor", "Capacity", "Avoid Floors"};
    private final int DEFAULT_MAX_CAPACITY = 20;
    
    private Elevator[] elevators;
    
    private short numOfElevators;
    private short totalFloors;
    private short maxFloor;
    
    ElevatorDialog(short nE, short floorCount){
        //floors = new Floor[nF];
        this.totalFloors = floorCount;
        this.numOfElevators = nE;
        this.maxFloor = (short)(floorCount-1);
        this.setTitle("Elevator set up");
        String[][] tableData = new String[this.numOfElevators][5];
        this.setModal(true);

        for (int i = 0; i < this.numOfElevators; i++){
            tableData[i][0] = "0";
            tableData[i][1] = String.valueOf(maxFloor);
            tableData[i][2] = "0";
            tableData[i][3] = String.valueOf(DEFAULT_MAX_CAPACITY);
            tableData[i][4] = "";
        }
        
        this.clearB = new JButton();
        this.clearB.addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                for (int i = 0; i < numOfElevators; i++){
                    tableWidget.setValueAt("", i, 0);
                }
                
            }
        }); 
        this.submitB = new JButton();
        this.submitB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                boolean close = true;
                elevators = new Elevator[numOfElevators];
        
                for (int i = 0; i < numOfElevators; i++){
                    short minFloor = Short.parseShort(tableWidget.getValueAt(i, 0).toString());
                    short maxFloor = Short.parseShort(tableWidget.getValueAt(i, 1).toString());
                    short stFloor = Short.parseShort(tableWidget.getValueAt(i, 2).toString());
                    byte maxCapacity = Byte.parseByte(tableWidget.getValueAt(i, 3).toString());
                    String[] banlist = tableWidget.getValueAt(i, 4).toString().split(" ");
                    boolean[] bl = new boolean[totalFloors];
                    if (!banlist[0].isEmpty()){
                        for (int j = minFloor; i < maxFloor; i++){
                            boolean allowed = true;
                            for (int k = 0; k < banlist.length; k++){
                                if (Short.parseShort(banlist[k]) == j){
                                    allowed = false;
                                } else {
                                    allowed = true;
                                }
                            }
                        bl[j] = allowed;
                        }
                    } else {
                        for (int j = 0; j < totalFloors; j++){
                            bl[j] = true;
                        }
                    }
                    if (minFloor < maxFloor && stFloor <= maxFloor && stFloor >= minFloor && maxCapacity <= 50){
                        elevators[i] = new Elevator(minFloor, maxFloor, stFloor, maxCapacity, bl);
                    } else {
                        close = false;
                    }
                }
                
                if (close){
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(new JDialog(), "Check all fields are correct, incl: Capacity <= 50.", "Warning", JOptionPane.WARNING_MESSAGE);
                }
            }
        }); 
        this.fillB = new JButton();
        this.fillB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                for (int i = 0; i < numOfElevators; i++){
                    tableWidget.setValueAt("0", i, 0);
                    tableWidget.setValueAt(String.valueOf(maxFloor), i, 1);
                    switch(i%3){
                        case 1:
                            tableWidget.setValueAt(String.valueOf(maxFloor), i, 2);
                            break;
                        case 2:
                            tableWidget.setValueAt(String.valueOf(java.lang.Math.round(maxFloor/2)), i, 2);
                            break;
                        default:
                            tableWidget.setValueAt(String.valueOf(0), i, 2);
                    }
                    tableWidget.setValueAt("20", i, 3);
                }
            }
        }); 
        
        this.fillB.setText("Fill");
        this.clearB.setText("Clear");
        this.submitB.setText("Set Elevators");
        //this.submitB.setDefault(true);
        
        this.getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
        GridLayout buttonGridLayout = new GridLayout(1,3);
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(buttonGridLayout);
        buttonPanel.setSize(350, 280);
        buttonPanel.add(clearB);
        buttonPanel.add(fillB);
        buttonPanel.add(submitB);
        
        
        this.tableWidget = new JTable(tableData, tableColumns);
        this.tableWidget.getTableHeader().setResizingAllowed(false);
        this.tableWidget.getTableHeader().setReorderingAllowed(false);
        tableView = new JScrollPane(tableWidget);
        tableWidget.setFillsViewportHeight(true);
    
        this.add(tableView);
        this.add(buttonPanel);
        this.setSize(350,300);
    }
    
    Elevator[] getElevators(){
        return elevators;
    }
    
    void setFloors(ArrayList<Floor> f){
        /*this.floors = f;
        for (int i = 0; i < this.floors.size(); i++){
            tableWidget.setItem(i, 0, new QTableWidgetItem(this.floors.get(i).getFloorName()));
        }*/
    }

}